﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using XCT.Class;
using XCT.Functions;
using System.Data.SqlClient;

namespace XCT.Forms
{
    public partial class FormSearch : Form
    {
        private FormMain formMain;
        public FormSearch(FormMain formMain)
        {
            InitializeComponent();
            this.formMain = formMain;
        }

        #region "Biến"

        //string pTuNgay;
        //string pDenNgay;
        //--------------
        string pCa;
        string pNgayGH;
        string pLSX; 
        string pMaSP; 

        #endregion
       
        #region đưa dữ liệu vào combo tuần tháng năm
        public void fKhoiTaoCombo()
        {
             //dateTuNgay.DateTime = DateTime.Now;
             //dateDenNgay.DateTime = DateTime.Now;
             //dateNgayGH.DateTime = DateTime.Now;
        }
        #endregion

        private void FormSearch_Load(object sender, EventArgs e)
        {
            fKhoiTaoCombo();
            KhoiTaogrdSearch(gridSearch_View);
        }

        //Khởi tạo gridSearch
        private void KhoiTaogrdSearch(DevExpress.XtraGrid.Views.Grid.GridView grdView)
        {
            grdView.Columns["TuNgay"].Visible = false;
            grdView.Columns["DenNgay"].Visible = false;
            grdView.Columns["Ca"].Visible = false;
            grdView.Columns["NgayTH"].Caption = "Ngày Thực Hiện";
            grdView.Columns["NgayGH"].Caption = "Ngày Giao Hàng";
            grdView.Columns["SL"].Caption = "Sản Lượng";
            grdView.Columns["solsx"].Caption = "Số LSX";
            grdView.Columns["masp"].Caption = "Mã SP";
            grdView.Columns["DoLy"].Caption = "Độ Ly";
            grdView.Columns["ChungLoai"].Caption = "Chủng Loại";
            grdView.Columns["Mau_Mousse"].Caption = "Màu Mousse";
            grdView.Columns["DOLY_HoaVan"].Caption = "Doly Hoa Văn";
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            
            //Bien chua gia tri truyen vao tham so trong Store
            pCa = "A";
            //pTuNgay = dateTuNgay.Text;
            //pDenNgay = dateDenNgay.Text;
            pNgayGH = dateNgayGH.Text;
            pLSX = txtLSX.Text;
            pMaSP = txtMaSP.Text;
            DataTable dt = new System.Data.DataTable();
            //De biet chinh xac chuoi Connection, hay tao moi mot DataBindingSource cho DatagridView, no se tu dong sinh ra chuoi nay, copy va paste vao la xong
            SqlConnection con = new SqlConnection("Data Source=192.168.4.2;Initial Catalog=KHKD;Integrated Security=True");
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            cmd.CommandText = "sp_SearchSL_CLLH";//Tên Store Proc
            //Khai bao cac tham so truyen vao Store Proc (neu co)
            cmd.Parameters.Add("@pCa", SqlDbType.Text);
            cmd.Parameters["@pCa"].Value = pCa;
            cmd.Parameters.Add("@pNgayGH", SqlDbType.Text);
            cmd.Parameters["@pNgayGH"].Value = pNgayGH;
            cmd.Parameters.Add("@pLSX", SqlDbType.Text);
            cmd.Parameters["@pLSX"].Value = pLSX;
            cmd.Parameters.Add("@pMaSP", SqlDbType.Text);
            cmd.Parameters["@pMaSP"].Value = pMaSP;
            //cmd.Parameters.Add("@pTuNgay", SqlDbType.Text);
            //cmd.Parameters["@pTuNgay"].Value = pTuNgay;
            //cmd.Parameters.Add("@pDenNgay", SqlDbType.Text);
            //cmd.Parameters["@pDenNgay"].Value = pDenNgay;
            //Lay data ra DataAdapter
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            //Do du lieu vao bang
            da.Fill(dt);
            gridSearch.DataSource = dt;
            con.Close();

            txtMaSP.Enabled = true;
            txtMaSP.Text = "";
            txtLSX.Enabled = true;
            txtLSX.Text = "";
            dateNgayGH.Enabled = true;
            dateNgayGH.Text = "";
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dateNgayGH_Click(object sender, EventArgs e)
        {
            txtMaSP.Enabled = false;
            txtLSX.Enabled = false;
        }

        private void txtLSX_Click(object sender, EventArgs e)
        {
            txtMaSP.Enabled = false;
            dateNgayGH.Enabled = false;
        }

        private void txtMaSP_Click(object sender, EventArgs e)
        {
            dateNgayGH.Enabled = false;
            txtLSX.Enabled = false;
        }
    }
}
